package com.xja.wechat.controller;

import com.xja.wechat.common.Constant;
import com.xja.wechat.common.JsonBean;
import com.xja.wechat.common.SHA1;
import com.xja.wechat.common.WechatSignature;
import com.xja.wechat.domain.model.WX_User;
import com.xja.wechat.service.WechatService;
import com.xja.wechat.service.WxUserService;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 微信服务器与项目交互控制器
 */
@RestController
public class WechatController {
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private WechatService wechatService;

    /**
     * 介入微信服务器，成为开发者，验证消息来自于微信
     *
     * @param wechatSignature 接受参数封装类
     * @return 随机字符串
     */
    @GetMapping("index")
    public String getIndex(WechatSignature wechatSignature) {
        System.out.println("get");
        /**
         * 验证消息 来自于微信服务器
         * @return
         */
        String token = "202003050948";
        List<String> list = new ArrayList<>();
        list.add(wechatSignature.getTimestamp());
        list.add(wechatSignature.getNonce());
        list.add(token);
        //字典排序
        Collections.sort(list);
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            str += list.get(i);
        }
        String sha1Str = SHA1.encode(str);
        if (sha1Str.equals(wechatSignature.getSignature())) {
            return wechatSignature.getEchostr();
        }
        return "error";
    }

    @PostMapping("index")
    public String postIndex(HttpServletRequest request) throws JDOMException, IOException {
        String resultXML = "";

        //通过request获取到xml流文件
        ServletInputStream inputStream = null;
        try {
            inputStream = request.getInputStream();
            //读取到文件获得整个xml文档
            Document document = new SAXBuilder().build(inputStream);
            //获取xml根节点
            Element rootElement = document.getRootElement();
            resultXML = wxUserService.updateWxMessage(rootElement);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultXML;
    }

    @PostMapping("code")
    public JsonBean code(String code, HttpServletRequest request) {
        JsonBean jsonBean = wechatService.oauth2SnsApi(code);
        request.getSession().setAttribute("wx_user", jsonBean.getData());
        return jsonBean;
    }

    @PostMapping("getSessionInfo")
    public JsonBean getSession(HttpServletRequest request) {
        WX_User wx_user = (WX_User) request.getSession().getAttribute("wx_user");
        System.out.println(wx_user.toString());
        return new JsonBean(0, "ok", wx_user);
    }

    /**
     * 分享到qq等等
     *
     * @return
     */
    @PostMapping("share")
    public JsonBean share(String targetUrl, String openid, HttpServletRequest request) {
        WX_User wx_user = (WX_User) request.getSession().getAttribute("wx_user");
        return wechatService.shareData(targetUrl, wx_user, openid);

    }
}
